﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NPMonitor.DataModel;
using NPMonitor.Implementation;
using Newtonsoft.Json;

namespace NPMonitor.Web.Handler
{
    /// <summary>
    /// Summary description for ReportHandler
    /// </summary>
    public class ReportHandler : IHttpHandler
    {

        #region Private Members

        private string Operation { get; set; }

        protected HttpRequest Request
        {
            get
            {
                return HttpContext.Current.Request;
            }
        }

        protected HttpResponse Response
        {
            get
            {
                return HttpContext.Current.Response;
            }
        }

        private HttpContext context;

        public string GetQueryValue(string name)
        {
            if (string.IsNullOrEmpty(Request.QueryString[name]))
                return "";
            else
                return Request.QueryString[name].Trim();
        }

        #endregion

        public void ProcessRequest(HttpContext context)
        {
            this.context = context;
            this.context.Response.ContentType = "text/plain";

            Operation = this.context.Request["op"];

            switch (Operation)
            {
                case "GetScenarioNO":
                    GetScenarioNO();
                    break;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private void GetScenarioNO()
        {
            string jobId = GetQueryValue("jobId");

            List<Scenario> scenarioList = new List<Scenario>();

            int jobid;
            if (Int32.TryParse(jobId, out jobid))
            {
                scenarioList = StoreManager.Current.ReportStore.GetScenarioNoByJobId(jobid);

                var jsonResult = JsonConvert.SerializeObject(scenarioList);

                Response.Write(jsonResult);
            }
        }
    }
}